package persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import negocio.Clase;
import negocio.Cursada;
import negocio.Examen;

public class ExamenDAO 
{
	private static ExamenDAO instancia;
	private final Connection c;
	
	private ExamenDAO()
	{
		c = BDManager.getInstance().getConexion();
	}

	public static ExamenDAO getInstance()
	{
		if(instancia ==null)
		{
			instancia = new ExamenDAO();
		}
		return instancia;
	}
	
	public void guardar(Examen examen)
	{
		String query = "INSERT INTO Examen(fechaexamen, cursada_fk, nombre) VALUES ('" + examen.getFecha().toString() + "'," + examen.getCursadaId() + ",'" + examen.getNombre() + "')";		
		try {
			Statement s = c.createStatement();
			s.execute(query);
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public ArrayList<Examen> getExamenes(Cursada cursada)
	{
		String query = "SELECT * FROM examen WHERE cursada_fk =" + cursada.getId();
		ArrayList<Examen> res = new ArrayList<Examen>();
		try {
			Statement s = c.createStatement();
			ResultSet rs = s.executeQuery(query);
			while(rs.next())
			{
				res.add(new Examen(rs.getInt(1), cursada, rs.getDate(2), rs.getString(4)));
			}
			
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return res;
	}
	

}
